package com.vicene.gmail.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.vicene.gmail.common.exception.CommonCodeEnum;
import com.vicene.gmail.ware.exception.NoStockException;
import com.vicene.gmail.ware.service.WareOrderTaskDetailService;
import com.vicene.gmail.ware.service.WareOrderTaskService;
import com.vicene.gmail.ware.vo.LockStockResult;
import com.vicene.gmail.ware.vo.SkusHasStockVo;
import com.vicene.gmail.ware.vo.WareSkuLockVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.vicene.gmail.ware.entity.WareSkuEntity;
import com.vicene.gmail.ware.service.WareSkuService;
import com.vicene.gmail.common.utils.PageUtils;
import com.vicene.gmail.common.utils.R;



/**
 * 商品库存
 *
 * @author vicene
 * @email 505543479@qq.com
 * @date 2020-10-16 15:21:32
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;


    /**
     * 库存解锁场景
     * 1》下单成功，订单过期没有支付，被用户手动取消
     * 2》下单成功，库存锁定成功，其他业务调用失败，订单回滚，之前锁定库存解锁
     * @param vo
     * @return
     */
    @RequestMapping("/lock/order")
    public R orderLockStock(@RequestBody WareSkuLockVo vo){
       try{
           Boolean stock = wareSkuService.orderLockStock(vo);
           return R.ok();

       } catch (NoStockException e){
            return R.error(CommonCodeEnum.NO_STOCK_EXISTS_EXCEPTION.getCode() , CommonCodeEnum.NO_STOCK_EXISTS_EXCEPTION.getMsg());
       }
    }

    @RequestMapping("/hasstock")
    //@RequiresPermissions("ware:waresku:list")
    public R getSkusHasStock(@RequestBody List<Long> skuIds){
        List<SkusHasStockVo> vos =  wareSkuService.getSkusHasStock(skuIds);
        R r = R.ok();
        return R.ok().put("data", vos);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("ware:waresku:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ware:waresku:info")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ware:waresku:save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ware:waresku:update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ware:waresku:delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
